<template>
  <el-submenu
    v-if="menu.children && menu.children.filter(item => !item.hidden).length >= 1"
    :index="menu.id + ''"
    :popper-class="'site-sidebar--' + sidebarLayoutSkin + '-popper'">
    <template slot="title">
      <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
      <span>{{ menu.name }}</span>
    </template>
    <sub-menu
      v-for="item in menu.children"
      :key="item.id"
      :menu="item"
      :dynamicMenuRoutes="dynamicMenuRoutes">
    </sub-menu>
  </el-submenu>
  <el-menu-item v-else-if="!menu.hidden" :index="menu.id + ''" @click="gotoRouteHandle(menu)">
    <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
    <span>{{ menu.name }}</span>
  </el-menu-item>
</template>

<script>
  import SubMenu from './main-sidebar-sub-menu'

  export default {
    name: 'sub-menu',
    props: {
      menu: {
        type: Object,
        required: true
      },
      dynamicMenuRoutes: {
        type: Array,
        required: true
      }
    },
    components: {
      SubMenu
    },
    computed: {
      sidebarLayoutSkin: {
        get () {
          return this.$store.state.common.sidebarLayoutSkin
        }
      },
      authList () {
        let authList = JSON.parse(sessionStorage.getItem('authList'))
        if (this.$store.state.authList instanceof Array && this.$store.state.authList.length < 1) {
          this.$store.dispatch('setAuthList', authList)
        }
        return this.$store.state.authList
      }
    },
    methods: {
      // 通过code与动态(菜单)路由进行匹配跳转至指定路由
      gotoRouteHandle (menu) {
        const route = this.dynamicMenuRoutes.filter(item => item.meta.id === menu.id)
        if (route.length >= 1) {
          this.$store.commit('user/SET_CURRENT_MENU', menu.id)
          this.$router.push({name: route[0].name}).catch(err => { console.log(err) })
        }
      }
    }
  }
</script>
